<?php

include_once 'include.php';
$template = new template("Communications");
$communications = new communications();
$menu = menu();
$use = $db->get_row("Select * from Users where id='".$_SESSION['id']."'");
$page = "list";
$id = 0;
$message = null;
if(isset ($_GET["page"]))$page = $_GET["page"];
if(isset ($_GET["id"]))$id = $_GET["id"];
//if(isset ($_GET["message"]))$message = $_GET["message"];
if(isLoggedIn())
{
    switch ($page) {
        case "addCommunication":
            $template->addContent($communications->addCommunication($id));
        break;

    case "delete":
            $communications->deleteCommunication($id);
        break;

    case "added":
            $communications->added();
        break;
        default:
            break;
    }
}
else
{
$template->addContent("You are not authorized to view this page. Please <a href = 'login.html'> Log In </a>");
}
$template->showMenu($menu);
$template->showLogin("Welcome");
$template->showUser($use->name.' | <a href="logout.php" class="logout">Logout</a>');
$template->genPage();
class communications
{
        function addCommunication($id)
        {
            $page = "<h2> Add Communication </h2><div class='form'>
         <form action='communications.php?page=added' method='post' class='niceform'>
         
                <fieldset>
                <input type ='hidden' value = '".$id."'name='id'/>
                    <dl>
                        <dt><label for='username'>Details:</label></dt>
                        <dd><textarea  name='details' id=''  rows='10' cols = '40'></textarea></dd>
                    </dl>
                    <dl class='submit'>
 <input type='submit' name='submit' id='submit' value='Save' /> </dl>";
            return $page;
        }
        
        function added()
        {
            Global $db;
            $id = $_POST['id'];
            
            $details = $_POST['details'];
            $query = "INSERT INTO communications (donorId, timestamp, details) VALUES (".$id.", Now(),'".$details."')";
            $db->query($query);
            
            header("Location:donor.php?page=item&id=".$id."&message=Communication Added");
        }
        
        function deleteCommunication($id)
        {
            Global $db;
            $did = $db->get_var("Select donorId FROM communications WHERE id=".$id);
            $this->deleteCommunications($id);
             header("Location:donor.php?page=item&id=".$did."&message=Communication Deleted");
        }
        
        
    function deleteReminders($id, $type)
    {
        Global $db;
        $db->query("DELETE FROM reminder WHERE typeId = ".$id." AND type=$type");
    }
    
    function deleteCommunications($id)
    {
        Global $db;
        $com = $db->get_results("Select * from communications WHERE id = ".$id);
        foreach ($com as $theCom)
        {
            $this->deleteReminders($theCom->id,3);
        }
        $db->query("DELETE FROM communications WHERE id = ".$id);
    }
    
}
?>
